<?php

class lm_page {

    var $page_name = "lmp"; //page标签，用来控制url页。比如说xxx.php?PB_page=2中的PB_page
    var $next_page = '>'; //下一页
    var $pre_page = '<'; //上一页
    var $first_page = '<<'; //首页
    var $last_page = '>>'; //尾页
    var $pre_bar = '<<'; //上一分页条
    var $next_bar = '>>'; //下一分页条
    var $format_left = '[';
    var $format_right = ']';
    var $is_ajax = false; //是否支持AJAX分页模式
    var $ajax_para = ''; //AJAX分页参数
    var $allNum = 0; //总条数
    var $pagebarnum = 9; //控制记录条的个数。
    var $totalpage = 0; //总页数
    var $ajax_action_name = ''; //AJAX动作名
    var $nowindex = 1; //当前页
    var $url = ""; //url地址头
    var $offset = 0;

    function lm_page($array) {
        if (is_array($array)) {
            if (!array_key_exists('total', $array))
                $this->error(__FUNCTION__, 'need a param of total');
            $total = intval($array['total']);
            $perpage = (array_key_exists('perpage', $array)) ? intval($array['perpage']) : 10;
            $page_name = (array_key_exists('page_name', $array)) ? $array['page_name'] : 'lmp';
            $nowindex = (array_key_exists('nowindex', $array)) ? intval($array['nowindex']) : '';
            $url = (array_key_exists('url', $array)) ? $array['url'] : '';
        } else {
            $total = $array;
            $perpage = 10;
            $nowindex = '';
            $url = '';
        }
        if ((!is_int($total)) || ($total < 0))
            $this->error(__FUNCTION__, $total . ' is not a positive integer!');
        if ((!is_int($perpage)) || ($perpage <= 0))
            $this->error(__FUNCTION__, $perpage . ' is not a positive integer!');
        if (!empty($array['page_name']))
            $this->set('page_name', $array['page_name']); //设置pagename
        $this->allNum = $total; //设置总条数
        $this->_set_nowindex($nowindex); //设置当前页
        $this->_set_url($url); //设置链接地址
        $this->totalpage = ceil($total / $perpage);
        $this->totalpage = $this->totalpage == 0 ? '1' : $this->totalpage;
        $this->offset = ($this->nowindex - 1) * $perpage;
        if (!empty($array['ajax']))
            $this->open_ajax($array['ajax']); //打开AJAX模式
        if (!empty($array['ajax_para']))
            $this->ajax_para = $array['ajax_para'] . ','; //打开AJAX模式
    }

    /**
     * 设定类中指定变量名的值，如果改变量不属于这个类，将throw一个exception
     *
     * @param string $var
     * @param string $value
     */
    function set($var, $value) {
        if (in_array($var, get_object_vars($this)))
            $this->$var = $value;
        else {
            $this->error(__FUNCTION__, $var . " does not belong to LM_Page!");
        }
    }

    /**
     * 打开倒AJAX模式
     *
     * @param string $action 默认ajax触发的动作。
     */
    function open_ajax($action) {
        $this->is_ajax = true;
        $this->ajax_action_name = $action;
    }

    /**
     * 获取显示"下一页"的代码
     *
     * @param string $style
     * @return string
     */
    function next_page($style = '') {
        if ($this->nowindex < $this->totalpage) {
            if ($this->is_ajax) {
                return $this->_get_text($this->_get_link($this->ajax_para . ($this->nowindex + 1), $this->next_page, $style));
            } else {
                return $this->_get_text($this->_get_link($this->_get_url($this->nowindex + 1), $this->next_page, $style));
            }
        }
        return $this->_get_text($this->next_page); //'<div style="float:left;width:20px;height:20px;border:1px solid #ddd;text-align:center;vertical-align:middle;">'.$this->_get_text($this->next_page).'</div>';
    }

    /**
     * 获取显示“上一页”的代码
     *
     * @param string $style
     * @return string
     */
    function pre_page($style = '') {
        if ($this->nowindex > 1) {
            if ($this->is_ajax) {
                return $this->_get_text($this->_get_link($this->ajax_para . ($this->nowindex - 1), $this->pre_page, $style));
            } else {
                return $this->_get_text($this->_get_link($this->_get_url($this->nowindex - 1), $this->pre_page, $style));
            }
        }
        return $this->_get_text($this->pre_page); //'<div style="float:left;width:20px;height:20px;border:1px solid #ddd;text-align:center;vertical-align:middle;">'.$this->_get_text($this->pre_page).'</div>';
    }

    /**
     * 获取显示“首页”的代码
     *
     * @return string
     */
    function first_page($style = '') {
        if ($this->nowindex == 1) {
            return $this->_get_text($this->first_page); //'<div style="float:left;display:inline;width:20px;height:20px;border:1px solid #ddd;text-align:center;vertical-align:middle;">'.$this->_get_text($this->first_page).'</div>';
        }
        if ($this->is_ajax) {
            return $this->_get_text($this->_get_link($this->ajax_para . '1', $this->first_page, $style));
        } else {
            return $this->_get_text($this->_get_link($this->_get_url(1), $this->first_page, $style));
        }
    }

    /**
     * 获取显示“尾页”的代码
     *
     * @return string
     */
    function last_page($style = '') {
        if ($this->nowindex == $this->totalpage) {
            return $this->_get_text($this->last_page); //'<div style="display:inline;width:20px;height:20px;border:1px solid #ddd;text-align:center;vertical-align:middle;">'.$this->_get_text($this->last_page).'<div>';
        }
        if ($this->is_ajax) {
            return $this->_get_text($this->_get_link($this->ajax_para . $this->totalpage, $this->last_page, $style));
        } else {
            return $this->_get_text($this->_get_link($this->_get_url($this->totalpage), $this->last_page, $style));
        }
    }

    function nowbar($style = '', $nowindex_style = '') {
        $plus = ceil($this->pagebarnum / 2);
        if ($this->pagebarnum - $plus + $this->nowindex > $this->totalpage)
            $plus = ($this->pagebarnum - $this->totalpage + $this->nowindex);
        $begin = $this->nowindex - $plus + 1;
        $begin = ($begin >= 1) ? $begin : 1;
        $return = '';
        for ($i = $begin; $i < $begin + $this->pagebarnum; $i++) {
            if ($i <= $this->totalpage) {
                if ($i != $this->nowindex) {
                    if ($this->is_ajax) {
                        $return .= $this->_get_text($this->_get_link($this->ajax_para . $i, $i, $style));
                    } else {
                        $return .= $this->_get_text($this->_get_link($this->_get_url($i), $i, $style));
                    }
                } else {
                    $return .= $this->_get_text('<div style="float:left;font-weight:bold;margin-left:3px;background:#717171;width:23px;padding:3px 0;color:#fff;">' . $i . '</div>');
                }
            } else {
                break;
            }
            $return .= "\n";
        }
        unset($begin);
        if ($i <= $this->totalpage) {
            $return.='<div style="float:left;width:20px;margin-top:5px;">...</div>';
        }
        return $return;
    }

    /**
     * 获取显示跳转按钮的代码
     *
     * @return string
     */
    function select($url) {
        $return = '<select name="PB_Page_Select"   >';
        for ($i = 1; $i <= $this->totalpage; $i++) {
            if ($i == $this->nowindex) {
                $return .= '<option value=' . $url . $i . ' selected>' . $i . '</option>';
            } else {
                $return .= '<option value=' . $url . $i . '>' . $i . '</option>';
            }
        }
        unset($i);
        $return .= '</select>';
        return $return;
    }

    /**
     * 获取mysql 语句中limit需要的值
     *
     * @return string
     */
    function offset() {
        return $this->offset;
    }

    /**
     * 控制分页显示风格（你可以增加相应的风格）
     *
     * @param int $mode
     * @return string
     */
    function show($mode = 1, $url = '') {
        switch ($mode) {
            case '1':
                return $this->first_page() . $this->pre_page() . $this->nowbar() . $this->next_page() . $this->last_page();
                break;
            case '2':

                return $this->first_page() . $this->pre_page() . '[第' . $this->nowindex . '页]' . $this->next_page() . $this->last_page() . '第' . $this->select($url) . '页';
                break;
            case '3':

                return $this->first_page() . $this->pre_page() . $this->next_page() . $this->last_page();
                break;
            case '4':

                return $this->pre_page() . $this->nowbar() . $this->next_page();
                break;
            case '5':
                return $this->first_page() . $this->pre_page() . $this->nowbar() . $this->next_page() . $this->last_page();
                break;
            case '6':
                /* 如果只有1页的,T了 */
                if ($totalpage == 1) {
                    return '';
                }

                return $this->first_page() . $this->pre_page() . $this->nowbar() . $this->next_page() . $this->last_page();
                break;
            case '7':
                /* 如果只有1页的,T了 */
                if ($totalpage == 1) {
                    return '';
                }

                return '<div style="float:left;display:inline;white-space:nowrap;width:auto;padding:3px 10px 1px 3px;height:25px;border:1px solid #D6D6D6;text-align:center;vertical-align:middle;background:#FCFCFC;">' . $this->first_page() . $this->pre_page() . $this->nowbar() . $this->next_page() . $this->last_page() . '</div>';
                break;
            case '8':
                /* 如果只有1页的,T了 */
                if ($totalpage == 1) {
                    return '';
                }

                return '<div style="float:left;display:inline;white-space:nowrap;width:auto;padding:0px 15px 0px 15px;height:25px;border:1px solid #D6D6D6;text-align:center;vertical-align:middle;background:#FCFCFC;">'
                . '<div style="float:left;text-align:center;width:auto;padding-top:5px;"><span style="color:red;float:left;">' . $this->allNum . '</span><div style="*padding-top:2px;float:left;">个回复</div></div>'
                . $this->first_page()
                . $this->nowbar()
                . $this->next_page()
                . $this->last_page()
                . '</div>';
                break;
            case '9':
                /* 如果只有1页的,T了 */
                if ($totalpage == 1) {
                    return '';
                }

                return '<div style="float:left;display:block;padding:0 15px;height:25px;white-space:nowrap;width:auto;border:1px solid #D6D6D6;text-align:center;vertical-align:middle;background:#FDFDFD;">'
                . '<div style="float:left;text-align:center;width:auto;line-height:20px;padding:3px;">共<span style="color:;">' . $this->allNum . '</span>个帖子</div>'
                . $this->first_page()
                . $this->nowbar()
                //.$this->next_page()
                . $this->last_page() //最后一页
                . '<div style="float:left;text-align:center;width:auto;line-height:20px;padding:3px 0 0 10px;">共<span style="color:;">' . $this->totalpage . '</span>页</div>'
                . '</div>'
                . '<div style="float:left;width:150px;height:22px;_height:23px;background:#FDFDFD;padding:3px 10px 0px 3px;border:1px solid #D6D6D6;border-left:0;">'
                . '<form action="javascript:void(0)" method="get" onsubmit="return jumpPage();">'
                . '<div style="float:left;padding-top:5px;margin:0 3px;">转到</div><input id="pageId" onfocus="this.focus();this.select();" type="text" value="' . $this->nowindex . '" style="float:left;text-align:center;width:50px;margin-top:2px;border:1px solid #9c9c9c;"/><div style="float:left;padding-top:5px;margin:0 5px;">页</div><input type="submit" value="GO" style="float:left;margin-top:1px;cursor:pointer;background:#FDFDFD;"/>'
                . '</form></div>'
                ;
                break;
            case '10':
                /* 如果只有1页的,T了 */
                if ($totalpage == 1) {
                    return '';
                }

                return '<div style="float:left;display:block;padding:0 15px;height:25px;white-space:nowrap;width:auto;border:1px solid #D6D6D6;text-align:center;vertical-align:middle;background:#FDFDFD;">'
                . '<div style="float:left;text-align:center;width:auto;line-height:20px;padding:3px;">共<span style="color:;">' . $this->allNum . '</span>个会员</div>'
                . $this->first_page()
                . $this->nowbar()
                //.$this->next_page()
                . $this->last_page() //最后一页
                . '<div style="float:left;text-align:center;width:auto;line-height:20px;padding:3px 0 0 10px;">共<span style="color:;">' . $this->totalpage . '</span>页</div>'
                . '</div>'
                . '<div style="float:left;width:150px;height:22px;_height:23px;background:#FDFDFD;padding:3px 10px 0px 3px;border:1px solid #D6D6D6;border-left:0;">'
                . '<form action="javascript:void(0)" method="get" onsubmit="return jumpPage();">'
                . '<div style="float:left;padding-top:5px;margin:0 3px;">转到</div><input id="pageId" onfocus="this.focus();this.select();" type="text" value="' . $this->nowindex . '" style="float:left;text-align:center;width:50px;margin-top:2px;border:1px solid #9c9c9c;"/><div style="float:left;padding-top:5px;margin:0 5px;">页</div><input type="submit" value="GO" style="float:left;margin-top:1px;cursor:pointer;background:#FDFDFD;"/>'
                . '</form></div>'
                ;
                break;
            case '12':
                /* 如果只有1页的,T了 */
                if ($totalpage == 1) {
                    return '';
                }

                $this->next_page = '下一页'; //下一页
                $this->pre_page = '上一页'; //上一页
                $this->first_page = '最前页'; //首页
                $this->last_page = '最后页'; //尾页
                $this->pre_bar = '<<'; //上一分页条
                $this->next_bar = '>>'; //下一分页条
                return '<div style="float:left;height:20px;line-height:20px;">第'
                . $this->nowindex
                . '/'
                . $this->totalpage
                . '页  共   '
                . $this->allNum
                . '  条记录</div>'
                . '<div style="float:left;margin-left:10px;width:40px;">'
                . $this->first_page("float:left;")
                . '</div><div style="float:left;width:40px;">'
                . $this->pre_page("float:left;")
                . '</div><div style="float:left;width:40px;">'
                . $this->next_page("float:left;")
                . '</div><div style="float:left;width:40px;">'
                . $this->last_page(";float:left;")
                . '</div>'
                ;
                break;
            case '11':
                /* 如果只有1页的,T了 */
                if ($totalpage == 1) {
                    return '';
                }
                $this->pre_page = '上一页'; //上一页
                $this->first_page = '首页'; //首页

                $this->last_page = '尾页'; //尾页
                $this->next_page = '下一页'; //下一页
                return $this->no_style_first_page()
                . $this->no_style_pre_page()
                . $this->no_style_nowbar()
                . $this->no_style_next_page()
                . $this->no_style_last_page();
                break;
        }
    }

    /* ----------------private function (私有方法)----------------------------------------------------------- */

    /**
     * 设置url头地址
     * @param: String $url
     * @return boolean
     */
    /*
      function _set_url($url = "")
      {
      if (! empty($url)) {
      //手动设置
      $this->url = $url.((stristr($url, '?'))?'&':'?').$this->page_name."=";
      } else {
      //自动获取
      if ( empty($_SERVER['QUERY_STRING'])) {
      //不存在QUERY_STRING时
      $this->url = $_SERVER['REQUEST_URI']."?".$this->page_name."=";
      } else {
      //
      if (stristr($_SERVER['QUERY_STRING'], $this->page_name.'=')) {
      //地址存在页面参数
      $this->url = str_replace($this->page_name.'='.$this->nowindex, '', $_SERVER['REQUEST_URI']);
      $last = $this->url[strlen($this->url)-1];
      if ($last == '?' || $last == '&') {
      $this->url .= $this->page_name."=";
      } else {
      $this->url .= '&'.$this->page_name."=";
      }
      } else {
      //
      $this->url = $_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
      }//end if
      }//end if
      }//end if
      }
     */
    function _set_url($url = "") {
        if (!empty($url)) {
            //手动设置
            $this->url = $url . ((stristr($url, '?')) ? '&' : '?') . $this->page_name . "=";
        } else {
            $url = explode('.', $_SERVER['HTTP_HOST']);
            $domain = '';
            global $createHtml;
            if (count($url) == 3 && $url[0] != 'www' && $createHtml != '' && strrpos($_SERVER['REQUEST_URI'], $url[0]) === false) {
                $domain = $url[0] . '.php?';
            }
            //自动获取
            if (empty($_SERVER['QUERY_STRING'])) {
                //不存在QUERY_STRING时
                if (strrpos($_SERVER['REQUEST_URI'], '.php') === false) {

                    $this->url = str_replace('.html', '.php?', $_SERVER['REQUEST_URI'] . $domain . $this->page_name . "=");
                } else {
                    $this->url = $_SERVER['REQUEST_URI'] . "?" . $this->page_name . "=";
                }
            } else {
                //
                $get = '';

                /**
                 * 获取url参数
                 * 循环当前页面url
                 */
                foreach ($_GET as $get_key => $get_val) {
                    if ($get_key != $this->page_name) {
                        /**
                         * 参数里面还有数组!
                         */
                        if (is_array($get_val)) {
                            foreach ($get_val as $g_v_k => $g_v_v) {
                                $get.=trim($get) == '' ? "{$get_key}[{$g_v_k}]={$g_v_v}" : "&{$get_key}[{$g_v_k}]={$g_v_v}";
                            }
                        } else {
                            $get.=trim($get) == '' ? $get_key . '=' . $get_val : '&' . $get_key . '=' . $get_val;
                        }
                    }
                }
                $get.=$get == '' ? $this->page_name . '=' : '&' . $this->page_name . '=';
                $this->url = $_SERVER["PHP_SELF"] . '?' . $get;
                /*
                  if (stristr($_SERVER['QUERY_STRING'], $this->page_name.'=')) {
                  //地址存在页面参数
                  $this->url = str_replace($this->page_name.''.$this->nowindex.'.html', '', $_SERVER['REQUEST_URI']);
                  $last = $this->url[strlen($this->url)-1];
                  if ($last == '?' || $last == '&') {
                  if(strrpos($_SERVER['REQUEST_URI'],'?')===false){
                  $this->url .= $this->page_name."";
                  }else{
                  $this->url .= $this->page_name."=";
                  }
                  } else {
                  if(strrpos($_SERVER['REQUEST_URI'],'?')===false){
                  $this->url .= ''.$this->page_name."";
                  }else{
                  $this->url .= '&'.$this->page_name."=";
                  }
                  }
                  } else {
                  //
                  if(strrpos($_SERVER['REQUEST_URI'],'?')===false){
                  $this->url = str_replace('.html','',$_SERVER['REQUEST_URI'].'-'.$this->page_name.'');
                  }else{
                  $this->url = $_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
                  }
                  }//end if
                 *
                 */
            }//end if
        }//end if
    }

    /**
     * 设置当前页面
     *
     */
    function _set_nowindex($nowindex) {
        if (empty($nowindex)) {
            //系统获取

            if (isset($_GET[$this->page_name])) {
                $this->nowindex = intval($_GET[$this->page_name]);
            }
        } else {
            //手动设置
            $this->nowindex = intval($nowindex);
        }
    }

    /**
     * 为指定的页面返回地址值
     *
     * @param int $pageno
     * @return string $url
     */
    function _get_url($pageno = 1) {
        return $this->url . $pageno;
    }

    /**
     * 获取分页显示文字，比如说默认情况下_get_text('<a href="">1</a>')将返回[<a href="">1</a>]
     *
     * @param String $str
     * @return string $url
     */
    function _get_text($str, $style='') {
        $style = (!empty($style)) ? $style : 'float:left;margin-left:5px;line-height:20px;width:20px;height:20px;word-break:keep-all;white-space:nowrap;text-align:center;vertical-align:middle;';

        return '
            <div
                style="' . $style . '"
                >'
        . $str .
        '</div>';
    }

    /**
     * 获取链接地址
     */
    function _get_link($url, $text, $style = '') {
        $style = (!empty($style)) ? $style : 'float:left;width:100%;height:100%;padding:3px 0;display:block;text-decoration:none;text-align:center;vertical-align:middle;';
        //$style = ( empty($style))?'':'class='.$style;
        if ($this->is_ajax) {
            //如果是使用AJAX模式
            return '<a

				href="javascript:void(0);"
				onclick="' . $this->ajax_action_name . '(' . $url . ')"
				>' . $text . '</a>';
            /* return '<a
              style=" '.$style.'"
              href="javascript:void(0);"
              onclick="'.$this->ajax_action_name.'('.$url.')"
              onMouseover="this.style.border=\'1px solid red\'"
              onMouseout="this.style.border=\'1px solid #DDDDDD\'"
              >'.$text.'</a>'; */
        } else {
            return '<a href="' . $url . '" >' . $text . '</a>';
            /* return '<a
              style=" '.$style.'"
              href="'.$url.'"
              onMouseover="this.style.border=\'1px solid red\'"
              onMouseout="this.style.border=\'1px solid #DDDDDD\'"
              >'.$text.'</a>'; */
        }
    }

    /**
     * 出错处理方式
     */
    function error($function, $errormsg) {
        die('Error in file <b>' . __FILE__ . '</b> ,Function <b>' . $function . '()</b> :' . $errormsg);
    }

    /**
     * 获取显示“首页”的代码
     *
     * @return string
     */
    function no_style_first_page() {
        if ($this->nowindex == 1) {
            $this->first_page="<span>".$this->first_page."</span>";
            return $this->first_page; //'$this->_get_text($this->first_page)';
        }
        if ($this->is_ajax) {
            return $this->_get_link($this->ajax_para . '1', $this->first_page, none);
        } else {
            return $this->_get_link($this->_get_url(1), $this->first_page, none);
        }
    }

    /**
     * 获取显示“尾页”的代码
     *
     * @return string
     */
    function no_style_last_page() {
        if ($this->nowindex == $this->totalpage) {
             $this->last_page="<span>".$this->last_page."</span>";
            return $this->last_page; //'$this->_get_text($this->last_page)';
        }
        if ($this->is_ajax) {
            return $this->_get_link($this->ajax_para . $this->totalpage, $this->last_page, none);
        } else {
            return $this->_get_link($this->_get_url($this->totalpage), $this->last_page, none);
        }
    }

    /**
     * 获取显示"下一页"的代码
     * 无样式版本
     * @return string
     */
    function no_style_next_page() {
        if ($this->nowindex < $this->totalpage) {
            if ($this->is_ajax) {
                return $this->_get_link($this->ajax_para . ($this->nowindex + 1), $this->next_page, none);
            } else {
                return $this->_get_link($this->_get_url($this->nowindex + 1), $this->next_page, none);
            }
        }else{
            $this->next_page="<span>".$this->next_page."</span>";
        }
        return '<a>' . $this->next_page . '</a>'; //'.$this->_get_text($this->next_page).';
    }

    /**
     * 获取显示“上一页”的代码
     * 无样式版本
     * @return string
     */
    function no_style_pre_page() {
        if ($this->nowindex > 1) {
            if ($this->is_ajax) {
                return $this->_get_link($this->ajax_para . ($this->nowindex - 1), $this->pre_page, none);
            } else {
                return $this->_get_link($this->_get_url($this->nowindex - 1), $this->pre_page, none);
            }
        }else{
            $this->pre_page = "<span>".$this->pre_page."</span>";
        }
        return '<a>' . $this->pre_page . '</a>'; //'.$this->_get_text($this->pre_page).';
    }

    /**
     * 获取显示“1,2,3,4”的代码
     * 无样式版本
     * @return string
     */
    function no_style_nowbar() {
        $plus = ceil($this->pagebarnum / 2);
        if ($this->pagebarnum - $plus + $this->nowindex > $this->totalpage)
            $plus = ($this->pagebarnum - $this->totalpage + $this->nowindex);
        $begin = $this->nowindex - $plus + 1;
        $begin = ($begin >= 1) ? $begin : 1;
        $return = '';
        for ($i = $begin; $i < $begin + $this->pagebarnum; $i++) {
            if ($i <= $this->totalpage) {
                if ($i != $this->nowindex) {
                    if ($this->is_ajax) {
                        $return .= $this->_get_link($this->ajax_para . $i, $i, none);
                    } else {
                        $return .= '<a href="' . $this->_get_url($i) . '">' . $i . '</a>';
                    }
                } else {
                    $return .= '<a class="on">' . $i . '</a>';
                }
            } else {
                break;
            }
            $return .= "\n";
        }
        unset($begin);
        if ($i <= $this->totalpage) {
            $return.='<span>...</span>';
        }
        return $return;
    }

}

?>
